home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / povray < prev    next >
Encoding:
Text File  |  2000-05-21  |  5.5 KB  |  189 lines

  1. #!/usr/bin/perl
  2.  
  3. use Gimp;
  4. use Gimp::Feature qw(gimp-1.1 persistency);
  5. use Gimp::Fu;
  6. use Gimp::Data;
  7.  
  8. N_"/Xtns/Render"; N_"/Xtns/Render/Logos"; N_"/Xtns/Render/Povray"; # i18n workaround
  9.  
  10. use constant DEG2RAD => 4 / 180 * atan2 1,1;
  11.  
  12. sub set_preferences {
  13.    $Gimp::Data{povray_preferences} = \@_;
  14.    ();
  15. }
  16.  
  17. sub get_preferences {
  18.    my $data;
  19.    while (!ref ($data=$Gimp::Data{povray_preferences})) {
  20.       Gimp->perl_fu_povray_preferences_set(RUN_INTERACTIVE,(undef)x3);
  21.    }
  22.    ($pov_path,$pov_quality,$pov_args)=@$data;
  23. }
  24.  
  25. register "povray_preferences_set",
  26.      "Set povray preferences",
  27.      "=pod(DESCRIPTION)",
  28.      "Marc Lehmann <pcg\@goof.com>",
  29.      "Marc Lehmann",
  30.      "19990803",
  31.      N_"<Toolbox>/Xtns/Render/Povray/Preferences...",
  32.      undef,
  33.      [
  34.           [PF_FILE,    "povray_path",    "The path to the povray executable",    "x-povray"],
  35.           [PF_STRING,    "quality",    "The quality setting (0..9, R)",    "R"],
  36.           [PF_STRING,    "extra_args",    "Extra arguments for povray invocation","+d"],
  37.          ],
  38.          \&set_preferences;
  39.  
  40. my @camera = (
  41.    [PF_SLIDER,    'cam_phi',    'The camera angle around the z axis',          0, [-180,180,1]],
  42.    [PF_SLIDER,    'cam_theta',    'The camera angle relative to the z-Axis',     0, [0,90,1]],
  43.    [PF_SLIDER,    'cam_radius',    'The camera distance',                 1, [0,25,0.3]],
  44.    [PF_SLIDER,    'cam_fov',    'The camera field-of-view',            30, [0,90,1]],
  45. );
  46.  
  47. sub get_camera(\@) {
  48.    (shift @{$_[0]},shift @{$_[0]},shift @{$_[0]},shift @{$_[0]});
  49. }
  50.  
  51. sub gen_camera {
  52.    my($p,$v,$r,$fov)=@_;
  53.    my($x,$y,$z);
  54.    $x = $r * sin ($v * DEG2RAD) * cos ($p * DEG2RAD);
  55.    $y = $r * sin ($v * DEG2RAD) * sin ($p * DEG2RAD);
  56.    $z = $r * cos ($v * DEG2RAD);
  57.    "camera { location <$x, $y, $z> angle $fov look_at <0,0,0> }";
  58. }
  59.  
  60. $prelude = <<I;
  61. #include "colors.inc"
  62. #include "textures.inc"
  63. #include "woods.inc"
  64. #include "skies.inc"
  65. #include "stars.inc"
  66. #include "stones.inc"
  67. #include "stoneold.inc"
  68. #include "golds.inc"
  69. /*#include "glass.inc"*/
  70. I
  71.  
  72. my @unlink;
  73.  
  74. sub cleanup {
  75.    unlink @unlink;
  76.    undef @unlink;
  77. }
  78.  
  79. END { cleanup }
  80.  
  81. sub run_povray {
  82.    my($w,$h,$script)=@_;
  83.    my($scr_path) = Gimp->temp_name("pov");
  84.    my($ppm_path) = Gimp->temp_name("ppm");
  85.    my($err_path) = Gimp->temp_name("err");
  86.    my($msg_path) = Gimp->temp_name("msg");
  87.    push @unlink, $scr_path, $ppm_path, $err_path, $msg_path;
  88.    open SCR, ">$scr_path" or die "Unable to create pov script '$scr_path': $!\n";
  89.    print SCR $prelude;
  90.    print SCR $script;
  91.    close SCR;
  92.    get_preferences,
  93.    my $cmd ="$pov_path +V -GS -GD -GR ".
  94.             "+GF$err_path +GW$msg_path ".
  95.             "+Q$pov_quality +i$scr_path $pov_args +FP +O$ppm_path +W$w +H$h";
  96.    open POV,"$cmd 2>&1 |" or die "Unable to run '$cmd': $!\n";
  97.    init Progress "Rendering...";
  98.    local $/ = "\r";
  99.    while (<POV>) {
  100.       for (split /\n/) {
  101.          if (/endering line\s+(\d+) of\s+(\d+)/) {
  102.             update Progress $1/$2;
  103.          } else {
  104.             #print "POV: $_\n";
  105.          }
  106.       }
  107.    }
  108.    my $res = close POV >> 8;
  109.    if (open ERR, "<$err_path") {
  110.       my $err = do { local $/; <ERR> };
  111.       close ERR;
  112.       $err =~ s/^\s+//; $err =~ s/\s+$//;
  113.       die "POVRAY ERROR OUTPUT:\n$err\n" if $err;
  114.    }
  115.    if (open MSG, "<$msg_path") {
  116.       my $err = do { local $/; <MSG> };
  117.       close MSG;
  118.       $err =~ s/^\s+//; $err =~ s/\s+$//;
  119.       Gimp->message("POVRAY WARNING OUTPUT:\n$err\n") if $err;
  120.    }
  121.    die "Povray returned with non-zero exit status ($res)\n" if $res;
  122.    -f $ppm_path or die "Povray produced no output image\n";
  123.  
  124.    $ppm_path;
  125. }
  126.  
  127. sub load_img {
  128.    my $img = Gimp->file_load((shift)x2);
  129.    $img->clean_all;
  130.    cleanup; # FIXME: remove when xs_exit repaired
  131.    $img;
  132. }
  133.  
  134. register "povray_render_texture",
  135.      "Render a povray texture into a new image",
  136.      "=pod(DESCRIPTION)",
  137.      "Marc Lehmann <pcg\@goof.com>",
  138.      "Marc Lehmann",
  139.      "19990803",
  140.      N_"<Toolbox>/Xtns/Render/Povray/Texture...",
  141.      "*",
  142.      [
  143.           @camera,
  144.           [PF_SPINNER,    "width",    "The resulting image width",    200, [1, 4096, 1]],
  145.           [PF_SPINNER,    "height",    "The resulting image height",    200, [1, 4096, 1]],
  146.           [PF_STRING,    'texture',    'The Povray texture name',    'T_Wood1'],
  147.           [PF_SLIDER,    "xscale",    "Horizontal Scale Factor", 1, [0.0001, 5, 0.1]],
  148.           [PF_SLIDER,    "yscale",    "Vertical Scale Factor", 1, [0.0001, 5, 0.1]],
  149.           [PF_SLIDER,    "rotation",    "Rotate about y (deg)", 0, [0, 360]],
  150.          ],
  151.          [PF_IMAGE],
  152.          sub {
  153.    my(@cam)=get_camera(@_);
  154.    my($w,$h,$texturename,$xscale,$yscale,$rotation)=@_;
  155.    load_img run_povray $w,$h,<<I . gen_camera @cam;
  156. #declare TileTexture = texture { $texturename scale <$xscale,$yscale,1> rotate $rotation * y }
  157. #declare TileSize = <1, 1, 1>;
  158.  
  159. #declare _TX_tempver = version;
  160. #declare _TX_size = TileSize * <1, 1, 1>;
  161. #declare TileSeam = 1;
  162. /*camera {location <.5, .5, -1> look_at <.5, .5, 0> orthographic up y right $aspectratio * x} */
  163.  
  164. #declare _TX_xtexture = texture {gradient x texture_map {
  165.    [.5 - (TileSeam / 2) TileTexture scale <1 / _TX_size.x, 1, 1>]
  166.    [.5 + (TileSeam / 2) TileTexture scale <1 / _TX_size.x, 1, 1> translate x]}}
  167. plane {z, 0 texture {gradient y texture_map {
  168.    [.5 - (TileSeam / 2) _TX_xtexture scale <1, 1 / _TX_size.y, 1>]
  169.    [.5 + (TileSeam / 2) _TX_xtexture scale <1, 1 / _TX_size.y, 1> 
  170.        translate y]}}}
  171.  
  172. light_source {z*100000 rgb <1, 1, 1>}
  173.  
  174. I
  175. };
  176.  
  177. exit main;
  178.  
  179. =head1 DESCRIPTION
  180.  
  181. No docs. Yet. Bug me to provide them.
  182.  
  183. =head1 ACK!
  184.  
  185. Thanks to Aaron Sherman who inspired me, to John Pitney who wrote some
  186. other, similar plug-in and to Adrian Likins who knew that. Not that this
  187. plug-in is cool enough to warrant a long list of thanks ;)
  188.  
  189.